package cn.nbt.busManage.model.vo;

import cn.nbt.busManage.model.dto.RoleDto;
import cn.nbt.busManage.model.dto.UserDto;
import cn.nbt.busManage.model.entity.Role;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.OneToMany;
import lombok.Getter;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Getter
public class UserVo implements Serializable {
    private final Integer id;
    private final String userName;
    private final List<RoleVo> roles;
    private final String password;
    private final String email;
    private final String phone;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime updateTime;

    public UserVo(UserDto userDto){
        this.id = userDto.getId();
        this.userName = userDto.getUserName();
        List<RoleDto> roleDtoList = userDto.getRoles();
        ArrayList<RoleVo> roleVos = new ArrayList<>();
        for (RoleDto roleDto : roleDtoList) {
            RoleVo roleVo = new RoleVo(roleDto);
            roleVos.add(roleVo);
        }
        this.roles = roleVos;
        this.password = "undefined";
        this.email = userDto.getEmail();
        this.phone = userDto.getPhone();
        this.createTime = userDto.getCreateTime();
        this.updateTime = userDto.getUpdateTime();
    }

    @JsonCreator
    public UserVo(@JsonProperty("id") Integer id,@JsonProperty("userName") String userName, @JsonProperty("roles") List<RoleVo> roles,@JsonProperty("password") String password,@JsonProperty("email") String email,@JsonProperty("phone") String phone,@JsonProperty("createTime") LocalDateTime createTime,@JsonProperty("updateTime") LocalDateTime updateTime) {
        this.id = id;
        this.userName = userName;
        this.roles = roles;
        this.password = password;
        this.email = email;
        this.phone = phone;
        this.createTime = createTime;
        this.updateTime = updateTime;
    }
}
